package com.withoutwords.ui.drawing;

import java.io.ByteArrayOutputStream;

import com.samsung.sdraw.AbstractSettingView;
import com.samsung.sdraw.SettingView;
import com.samsung.spensdk.SCanvasConstants;
import com.samsung.spensdk.SCanvasView;
import com.samsung.spensdk.applistener.SCanvasInitializeListener;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;

public class SamsungDrawingController extends BaseDrawingController implements SCanvasInitializeListener {

	protected SCanvasView mCanvasView;
	protected SettingView mSettingView;
	@Override
	public void initializeViews(Context context, ViewGroup parent,boolean bViewOnly) {
		super.initializeViews(context, parent, bViewOnly);
		
		if(mCanvasView != null)
		{
			mCanvasView.clearSCanvasView();
		}
		mCanvasView = new SCanvasView(mContext);
		bViewOnlyMode = bViewOnly;
		// mSettingView = new SettingView(this);

		mCanvasView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		// mSettingView.setLayoutParams(new LayoutParams(
		// LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

		mCanvasView.setSCanvasInitializeListener(this);

		parent.addView(mCanvasView);
		if(!bViewOnlyMode)
		{
			mSettingView = new SettingView(mContext);
			mSettingView.setLayoutParams(new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			parent.addView(mSettingView);
			
			mCanvasView.setSettingView(mSettingView);
		}
	}

	@Override
	public void undo() {
		mCanvasView.undo();

	}

	@Override
	public void onEraseModeActiviated() {
		mCanvasView.setCanvasMode(SCanvasConstants.SCANVAS_MODE_INPUT_ERASER);
		//if (mEraseBtn.isSelected())
		{
			if (mSettingView
					.isShown(AbstractSettingView.ERASER_SETTING_VIEW)) {
				mSettingView.closeView();
			} else {
				mSettingView
						.showView(AbstractSettingView.ERASER_SETTING_VIEW);
			}
		} /*else {
			mEraseBtn.setSelected(true);
			mSettingBtn.setSelected(false);
			if (mSettingView
					.isShown(AbstractSettingView.PEN_SETTING_VIEW)) {
				mSettingView.closeView();
			}
		}*/

	}

	@Override
	public void onSettingsRequested() {
		mCanvasView.setCanvasMode(SCanvasConstants.SCANVAS_MODE_INPUT_PEN);
		if (mSettingView
				.isShown(AbstractSettingView.PEN_SETTING_VIEW)) {
			mSettingView.closeView();
		} else {
			mSettingView
					.showView(AbstractSettingView.PEN_SETTING_VIEW);
		}
		/*if (mSettingBtn.isSelected()) {
			if (mSettingView
					.isShown(AbstractSettingView.PEN_SETTING_VIEW)) {
				mSettingView.closeView();
			} else {
				mSettingView
						.showView(AbstractSettingView.PEN_SETTING_VIEW);
			}
		} else {
			mSettingBtn.setSelected(true);
			mEraseBtn.setSelected(false);
			if (mSettingView
					.isShown(AbstractSettingView.ERASER_SETTING_VIEW)) {
				mSettingView.closeView();
			}
		}*/

	}

	@Override
	public byte[] getDrawingBytesToSend() {
		Bitmap bmp = mCanvasView.getBitmap(false);
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		Bitmap smallBitmap  = Bitmap.createScaledBitmap(bmp, sendingBitmapWidth,sendingBitmapHeight, true);
		if (smallBitmap != null && !smallBitmap.isRecycled())
			smallBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
		//smallBitmap.compress(Bitmap.CompressFormat.PNG, 50, stream);
		return stream.toByteArray();
	}

	@Override
	public void setCurrentDrawing(byte[] drawingData) {
		Bitmap smallBitmap = BitmapFactory.decodeByteArray(drawingData, 0,
				drawingData.length);
		Bitmap bmp = Bitmap.createScaledBitmap(smallBitmap,drawingBitmapWidth,drawingBitmapHeight, true);
		mCanvasView.setBitmap(bmp, true);
		mCanvasView
				.setCanvasMode(SCanvasConstants.SCANVAS_MODE_INPUT_IMAGE);
		
		smallBitmap.recycle();

	}

	@Override
	public void onInitialized() {
		mCanvasView.setBGColor(Color.TRANSPARENT);
		
	}

	@Override
	public void clearCurrentDrawing() {
		try
		{
		mCanvasView.clearSCanvasView();
		}
		catch (Exception e) {
			// TODO: handle exception
		}
		
	}

	@Override
	public byte[] getDrawingBytesToShare() {
		return addDrawingtoBackground(mContext, mCanvasView.getBitmap(false));
	}

}
